var uniqueItems = function (data, key) {
    var result = [];

    for (var i = 0; i < data.length; i++) {
        var value = data[i][key];

        if (result.indexOf(value) == -1) {
            result.push(value);
        }

    }
    return result;
};


hfrApp.registerCtrl("SitemapManagementController", function ($scope, $rootScope, $http, $window, HfrConfig, HfrStorage, HfrInit) {
	//Init message alert
	HfrInit.message();
	
	//Display meta header
	HfrConfig.displayMeta("siteManagement");
	
	$scope.showFilter = false;
	$scope.showSelector = {};
	$scope.selectors = {};
	$scope.checkTopic = {};
	$scope.showBlogs = {};
	
	//Change show filter
	$scope.show_filter = function () {
		if ($scope.showFilter) {
			$scope.showFilter = false;
		} else {
			$scope.showFilter = true;
		}
	};
	
	//Get all topic
	var topicUrl = HfrConfig.ApiServiceUrl.topicsUrlApi + "?customerId=" + $rootScope.username;
	$http.get(topicUrl)
		.success(function (data) {
			if (data.status == 200) {
				$scope.topics = data.response.topics;
				//Set default checked
				angular.forEach($scope.topics, function(topic, key) {
					$scope.checkTopic[topic.id] = true;
		    	});
			} else {
				$scope.topics = {};
				$rootScope.message.error = true;
				$rootScope.message.text = data.message;
			}
			
		})
		.error(function (data) {
			$scope.topics = {};
			$rootScope.message.error = true;
			$rootScope.message.text = "Can't connect to server ";
		});
	
	//Get all blog by customer
	var blogUrl = HfrConfig.ApiServiceUrl.blogsUrlApi + "?customerId=" + $rootScope.username;

	$http.get(blogUrl)
		.success(function (data) {
			if (data.status == 200) {
				$scope.blogs = data.response.blogs;
				//Init filter blog
				$scope.initFilter();
			} else {
				$scope.blogs = {};
				$scope.showBlogs = {};
				$rootScope.message.error = true;
				$rootScope.message.text = data.message;
			}
			
		})
		.error(function (data) {
			$scope.blogs = {};
			$scope.showBlogs = {};
			$rootScope.message.error = true;
			$rootScope.message.text = "Can't connect to server ";
		});
	
	$scope.viewSitemap = function (blogId) {
		if ($scope.showSelector[blogId]) {
			$scope.showSelector[blogId] = false;
		} else {
			if ($scope.showSelector[blogId] == undefined) {
				$scope.showSelector = {};
				$scope.showSelector[blogId] = true;
				
				$scope.selectors = {};
				
				$rootScope.blogId = blogId;
				
				HfrStorage.set("blogId", blogId);
				
				var selectorUrl = HfrConfig.ApiServiceUrl.blogsUrlApi + "/" + encodeURI(blogId) + "/selectors";
				console.log(selectorUrl)
				$http.get(selectorUrl)
					.success(function(data) {
						if (data.status == 200) {
							$scope.selectors[blogId] = data.response.selectors;
						} else {
							$scope.selectors = {};
						}
					})
					.error(function (data) {
						$rootScope.message.error = true;
						$rootScope.message.text = "Can't connect to server ";
					});
			} else {
				$scope.showSelector = {};
				$scope.showSelector[blogId] = true;
			}
			
		}
		
		return false;
	};
	
	//Event Add Sitemap
	$scope.addSitemap = function () {
		delete $rootScope.selectorId;
		HfrStorage.remove("selectorId");
		
		$window.location.href= "#/add-sitemap";
		
		return false;
	};
	
	//Event Edit Sitemap
	$scope.editSitemap = function (id) {
		$rootScope.selectorId = id;
		HfrStorage.set("selectorId", id);
		
		$window.location.href= "#/edit-sitemap";
		
		return false;
	};
	
	
	$scope.initFilter = function () {
		$scope.$watch(function () {
	        return {
	        	checkTopic: $scope.checkTopic,
	        	siteType: $scope.siteType
	        }
	    }, function () {
	    	$scope.showBlogs = {};
	    	angular.forEach($scope.blogs, function(blog, key) {
		    	for (var j in $scope.checkTopic) {
		    		var display = false; 
		    		if ($scope.siteType == undefined || $scope.siteType == "All") {
		    			display = true;
		    		} else if ($scope.siteType == blog.type) {
						display = true;
					}
		    		
		    		//Kiem tra xem blog co thuoc topic day khong
//		    		if (display && $scope.checkTopic[j]) {
//		    			display = true;
//		    		}
		    		
		    		if (display) {
		    			$scope.showBlogs[blog.url] = blog;
		    			break;
		    		}
		    	}
	    	});
	    }, true);
	};
});
